/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the GPL
 * version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.model;

import java.util.ArrayList;
import java.util.List;

import org.digitalsoul.switchplugin.actions.Action;
import org.digitalsoul.switchplugin.matchers.Matcher;

public class SwitchItem implements IConfigSwitchItem {

    private final String name;
    private int matches;
    private final List<Matcher> matchers;
    private final List<Action> actions;

    public SwitchItem(String name) {
        this.name = name;
        actions = new ArrayList<Action>();
        matchers = new ArrayList<Matcher>();
    }

    /**
     * @see org.digitalsoul.switchplugin.model.ISwitchItem#matches(org.digitalsoul.switchplugin.model.ISwitchFile)
     */
    public boolean matches(ISwitchFile file) {
        matches = 0;
        for (Matcher matcher : getMatchers()) {
            if (matcher.match(file)) {
                matches++;
            }
            else {
                break;
            }
        }
        return matches == getMatchers().size();
    }

    public int getLastNumberOfMatches() {
        return matches;
    }

    /**
     * @see org.digitalsoul.switchplugin.model.ISwitchItem#applyActions(org.digitalsoul.switchplugin.model.ISwitchFile)
     */
    public ISwitchFile applyActions(ISwitchFile file) {
        for (Action action : getActions()) {
            file = action.applyAction(file);
        }
        return file;
    }

    public void addMatcher(Matcher matcher) {
        matchers.add(matcher);
    }

    public void addAction(Action action) {
        actions.add(action);
    }

    public List<Matcher> getMatchers() {
        return matchers;
    }

    public List<Action> getActions() {
        return actions;
    }

    @Override
    public String toString() {
        return getName();
    }

    public String getName() {
        return name;
    }
}
